{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "14d988e6-5488-4011-85df-68d62ad2fa19",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如何执行label-only攻击，以CIFAR10为例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "332ec18f-8c05-4c88-aaed-6c0d0830b9dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torch.utils.data import DataLoader\n",
    "from torch.utils.data import Dataset\n",
    "from torchvision import datasets\n",
    "from torchvision import transforms\n",
    "from torchvision.transforms import ToTensor\n",
    "import torchvision.transforms as tt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import metrics\n",
    "\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bf230f27-983a-4069-9831-371e18ddbbf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入自己创建的python文件\n",
    "import sys\n",
    "sys.path.append(\"..\") # Adds higher directory to python modules path.\n",
    "from frame.DataProcess import CustomDataset, load_CIFAR10_keep\n",
    "from frame.TrainUtil import *\n",
    "from frame.LIRAAttack import *\n",
    "from frame.AttackUtil import evaluate_ROC\n",
    "from frame.ShadowAttack import shadow_attack\n",
    "from frame.ThresholdAttack import loss_threshold_attack, conf_threshold_attack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "07314e32-2b3a-422c-b559-f0deedb32b7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "LEARNING_RATE = 1e-3\n",
    "BATCH_SIZE = 64\n",
    "MODEL = 'CNN'\n",
    "EPOCHS = 50\n",
    "DATA_NAME = 'CIFAR10' \n",
    "weight_dir = os.path.join('..', 'weights', DATA_NAME)\n",
    "num_shadowsets = 50\n",
    "seed = 0\n",
    "prop_keep = 0.5\n",
    "\n",
    "model_transform = transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize(mean=[0.507, 0.487, 0.441], std=[0.267, 0.256, 0.276])\n",
    "    ])\n",
    "attack_transform = transforms.Compose([])\n",
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "\n",
    "# 影子模型攻击相关参数\n",
    "sha_models = [1,2,3] #[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]\n",
    "tar_model = 0\n",
    "attack_class = False #是否针对每个类别分别攻击\n",
    "attack_lr = 5e-4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "822b5a48-65e3-49fd-9935-f11cd41cb7df",
   "metadata": {},
   "outputs": [],
   "source": [
    "loss_fn = nn.CrossEntropyLoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4ccb0db7-1d79-44c2-bc94-3498102c8c4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Files already downloaded and verified\n",
      "Files already downloaded and verified\n"
     ]
    }
   ],
   "source": [
    "batch_size = BATCH_SIZE\n",
    "# 加载数据集\n",
    "x_train_data, y_train_data, train_keep = load_CIFAR10_keep(num_shadowsets, prop_keep=0.5, seed=0)\n",
    "train_data = CustomDataset(x_train_data, y_train_data, model_transform)\n",
    "train_dataloader = DataLoader(train_data, batch_size=batch_size, shuffle=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "137c9434-99fe-442c-b8e3-f31a9c051d64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CNN(\n",
       "  (conv1): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "  (conv2): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "  (conv3): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "  (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "  (fc1): Linear(in_features=1024, out_features=500, bias=True)\n",
       "  (fc2): Linear(in_features=500, out_features=10, bias=True)\n",
       "  (BatchNorm1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "  (BatchNorm2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       "  (BatchNorm3): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
       ")"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 加载出目标模型\n",
    "# 模型创建\n",
    "model = MODEL\n",
    "data_name = DATA_NAME\n",
    "epochs = EPOCHS\n",
    "if model in ['NN', 'NN_4layer']:\n",
    "    Target_Model = globals()['create_{}_model'.format(model)](x_train_data.shape[1], y_train_data.max()+1)\n",
    "elif model == 'CNN':\n",
    "    Target_Model = globals()['create_{}_model'.format(model)](y_train_data.max()+1, data_name)\n",
    "# 加载参数\n",
    "weight_path = os.path.join(weight_dir, \"{}_{}_epoch{}_model{}.pth\".format(data_name, model, epochs, tar_model))\n",
    "Target_Model.load_state_dict(torch.load(weight_path))\n",
    "Target_Model.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b679a278-34b4-4855-bbff-7361c4d20382",
   "metadata": {},
   "outputs": [],
   "source": [
    "# label-only攻击方法：\n",
    "# 输入：目标模型，目标数据集，噪声样本量，噪声大小\n",
    "def Label_attack(dataloader, target_model, loss_fn, device, sigma_list, nums):    \n",
    "    result_list = []\n",
    "    target_result = noise_infer(dataloader, target_model, loss_fn, device, sigma=0)\n",
    "    for sigma in sigma_list:\n",
    "        for i in range(nums):\n",
    "            pred_result = noise_infer(train_dataloader, Target_Model, loss_fn, device, sigma=sigma)\n",
    "            pred_result = pred_result.detach().cpu().numpy()\n",
    "            result_list.append(pred_result)\n",
    "\n",
    "    result_list = np.array(result_list)\n",
    "    result_sum = np.sum(result_list, axis=0)\n",
    "    target_result = target_result.detach().cpu().numpy()\n",
    "    noise_result = np.multiply(target_result, result_sum)\n",
    "    noise_result = noise_result/(nums*len(sigma_list))\n",
    "    pred_result = (noise_result>=np.median(noise_result))\n",
    "    return pred_result, noise_result\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a128b18-bd8f-4f9d-81ae-4f48b24f7ab2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基线攻击\n",
    "def base_attack(dataloader, target_model, loss_fn, device):    \n",
    "    pred_result = noise_infer(dataloader, target_model, loss_fn, device, sigma=0)\n",
    "    pred_result = pred_result.detach().cpu().numpy()\n",
    "    return pred_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4c28b81-bbe3-4dcb-810a-50c6cdc48e61",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9f4575ed-af16-48ce-aa55-80d45550b3bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = 100\n",
    "sigma_list = [0.15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0a72f524-ed68-413e-a3c9-c7ab2c8d2e71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test Error: \n",
      " Accuracy: 84.2%, Avg loss: 1.143888 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "pred_result = base_attack(train_dataloader, Target_Model, loss_fn, device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e572aaea-16f2-4e9b-a2ac-aca7464fb868",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6376"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accuracy = metrics.accuracy_score(train_keep[0], pred_result)\n",
    "accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a628055c-89cb-4441-91aa-1ba92772fa78",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "91d9afc7-c00f-477d-bb62-15c8709ea913",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test Error: \n",
      " Accuracy: 84.2%, Avg loss: 1.143888 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.004461 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.008660 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.995478 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.991620 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.999302 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.993186 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.993614 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.997833 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.3%, Avg loss: 1.984893 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.992333 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.998399 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.2%, Avg loss: 1.987797 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.988630 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.999323 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.995958 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.8%, Avg loss: 2.013004 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.983668 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.998830 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.2%, Avg loss: 1.991030 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.995103 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.2%, Avg loss: 1.992629 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.988063 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.001603 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.2%, Avg loss: 1.992244 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.999407 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.003771 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.005847 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.8%, Avg loss: 2.004925 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.8%, Avg loss: 2.011678 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.999120 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.995071 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.3%, Avg loss: 2.000104 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.996928 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.003536 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.003417 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.2%, Avg loss: 1.986312 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.001086 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.3%, Avg loss: 1.993024 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.006887 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 2.000512 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.001256 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.982190 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.988305 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.006554 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.996396 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.005144 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.996897 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 2.003205 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.989451 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.993422 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 2.001771 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.999662 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.990263 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.008990 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.993525 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.983234 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.990966 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.989343 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 2.002666 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.998400 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.990203 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.002285 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.998282 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.997806 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.999392 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.007105 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.007405 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.999985 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.000080 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.998089 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.983117 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.011013 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.005858 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.983259 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.013601 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.002187 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.997338 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.2%, Avg loss: 1.997662 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.995696 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 2.007922 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.8%, Avg loss: 1.990694 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.993303 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.990976 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.989370 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.988778 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.987966 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 1.993691 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.004318 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.2%, Avg loss: 1.995449 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 2.001960 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.001994 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.2%, Avg loss: 1.995513 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.988926 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.996366 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 1.997081 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.8%, Avg loss: 1.999590 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.002928 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.1%, Avg loss: 1.997468 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 73.0%, Avg loss: 2.001809 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 72.9%, Avg loss: 2.001605 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "pred_result, noise_result = Label_attack(train_dataloader, Target_Model, loss_fn, device, sigma_list, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "de86bd6c-a79b-4592-94bb-c4ce55c1a3ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.59752"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accuracy = metrics.accuracy_score(train_keep[0], pred_result)\n",
    "accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a06fd93-c61a-4a1d-a936-827a79caff20",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "002f6384-b0fa-4288-bce5-e564eca221f5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f9c71d4-23f5-4f16-b194-1581009ea017",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a15e197-d97a-423e-9a1b-a17b574238a3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55684e07-d31f-442d-8c99-ba0709dabe11",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c0d81b04-5d52-46b6-9e39-3bae01ebb1f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行noise攻击流程\n",
    "# 两个参数，ums，sigma\n",
    "# \n",
    "nums = 100\n",
    "sigma_list = [0.15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "beeddb0e-2077-4e19-88ab-82c7549fd83b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test Error: \n",
      " Accuracy: 84.2%, Avg loss: 1.143888 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.497650 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.8%, Avg loss: 1.491575 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.495103 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.499102 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.494899 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.499514 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.494504 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.502509 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.506963 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.490606 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.500382 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.504908 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.514092 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.498495 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.492791 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.506279 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.496584 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.496440 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.502370 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.8%, Avg loss: 1.493805 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.507618 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.497298 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.493250 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.498180 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.487854 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.493149 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.495062 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.492025 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.507324 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.504155 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.502548 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.500478 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.502143 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.501053 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.4%, Avg loss: 1.503596 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.500876 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.501243 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.500173 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.507036 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.501064 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.497233 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.494678 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.498372 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.500838 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.497354 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.507275 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.8%, Avg loss: 1.494864 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.493511 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.500876 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.8%, Avg loss: 1.497085 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.499711 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.498666 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.507069 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.501542 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.493143 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.493829 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.489980 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.494600 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.493315 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.508000 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.493372 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.497380 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.501722 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.495980 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.510866 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.504654 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.498655 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.499541 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.507796 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.501098 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.507042 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.503793 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.500723 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.498777 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.493843 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.497348 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.503126 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.4%, Avg loss: 1.502287 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.489494 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.4%, Avg loss: 1.504474 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.501692 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.501203 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.498610 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.507765 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.487598 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.8%, Avg loss: 1.496390 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.494670 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.502416 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.493916 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.511007 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.495900 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.507258 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.493301 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.500459 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.5%, Avg loss: 1.504679 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.8%, Avg loss: 1.493728 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.500674 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.488923 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.7%, Avg loss: 1.497819 \n",
      "\n",
      "Test Error: \n",
      " Accuracy: 78.6%, Avg loss: 1.503967 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "result_list = []\n",
    "target_result = noise_infer(train_dataloader, Target_Model, loss_fn, device, sigma=0)\n",
    "for sigma in sigma_list:\n",
    "    for i in range(nums):\n",
    "        pred_result = noise_infer(train_dataloader, Target_Model, loss_fn, device, sigma=sigma)\n",
    "        pred_result = pred_result.detach().cpu().numpy()\n",
    "        result_list.append(pred_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "df9b2b5b-f41a-42d7-8ad7-45d649af94b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "result_list = np.array(result_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "59452c9e-0df5-4077-a8fc-e3c8a33354e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "result_sum = np.sum(result_list, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "148fde21-6c07-4693-82df-b79c13097d08",
   "metadata": {},
   "outputs": [],
   "source": [
    "target_result = target_result.detach().cpu().numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2abd79f8-963e-4080-ac20-c76bcf4e6136",
   "metadata": {},
   "outputs": [],
   "source": [
    "noise_result = np.multiply(target_result, result_sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "805d84cd-4bb2-4144-8a56-0371b526f822",
   "metadata": {},
   "outputs": [],
   "source": [
    "noise_result = noise_result/100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a976133a-49b1-4922-aca0-bee7e6be856c",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred_result = (noise_result==1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de95d9f1-cbe5-42b7-9a04-eb8dec18515f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "977321e9-8b6d-42eb-be1a-147d3fcdaab1",
   "metadata": {},
   "outputs": [],
   "source": [
    "accuracy = metrics.accuracy_score(train_keep[0], pred_result)\n",
    "accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8eaf474a-b4a3-4500-88fe-0183f401e24e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14afa5f3-eff2-4d5b-a030-c6a9446133c0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8656a281-de79-4b39-94d2-84d481dc8986",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "723d3581-95d1-42f7-b044-34c82f9b037e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "921d4fdf-69f3-4fb0-b620-076aad5ab7a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b248ffe-83d1-43f4-a751-15c3da890611",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# 技术方案\n",
    "# 1给数据集统一加噪音\n",
    "# 2传入，得到预测结果矩阵\n",
    "# 3做循环\n",
    "# 4预测结果矩阵求均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73b730fd-bfba-4885-bdf7-ce85b2a0f5d1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38fca045-fa00-424f-adc0-678ef9bf6b56",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a95587b-0f41-47ce-b920-821aeac5e3ff",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "050f4261-b404-4a3f-ae74-c7612fcea8b4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad0d25dd-e08d-455f-b130-41e511a433d9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56b91e13-679c-48a5-9bc1-c4d23eb9f57b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e010bbd9-eba1-47a1-89b7-e1a064c80390",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "opacus",
   "language": "python",
   "name": "opacus"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
